﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SpaceShooter.Components;
using SpaceShooter.Extensions;
using Microsoft.Xna.Framework;

namespace SpaceShooter.ParticleSystem
{
    public class ParticleSystemManager
    {
        private static object locker = new object();
        private static ParticleSystemManager instance;

        private IList<ParticleSystem> particleSystems = new List<ParticleSystem>();
        private CompositeComponent comps;

        private ParticleSystem trailParticleSystem;
        private ParticleSystem explosionParticleSystem;
        private ParticleSystem explosionSmokeParticleSystem;

        public static void RegisterParticleSystems(Game game)
        {
            lock (locker)
            {
                if (instance == null)
                    instance = new ParticleSystemManager();
                else
                    throw new Exception("ParticleSystemManager is already initialized!");
            }

            instance.comps = new CompositeComponent(game);

            instance.explosionSmokeParticleSystem = new ParticleSystem(game, game.Content, @"ParticleSystem\ExplosionSmokeSettings");
            instance.explosionParticleSystem = new ParticleSystem(game, game.Content, @"ParticleSystem\ExplosionSettings");
            instance.trailParticleSystem = new ParticleSystem(game, game.Content, @"ParticleSystem\ProjectileTrailSettings");

            instance.comps.Add(instance.trailParticleSystem);
            instance.comps.Add(instance.explosionSmokeParticleSystem);
            instance.comps.Add(instance.explosionParticleSystem);

            instance.particleSystems.Add(instance.trailParticleSystem);
            instance.particleSystems.Add(instance.explosionSmokeParticleSystem);
            instance.particleSystems.Add(instance.explosionParticleSystem);

            game.Components.Add(instance.comps);
        }

        public static void Clear()
        {
            AllParticleSystems.Foreach(p => p.Clear());
        }

        public static IEnumerable<ParticleSystem> AllParticleSystems
        {
            get { return instance.particleSystems; }
        }

        public static ParticleSystem TrailParticleSystem
        {
            get { return instance.trailParticleSystem; }
        }

        public static ParticleSystem ExplosionParticleSystem
        {
            get { return instance.explosionParticleSystem; }
        }

        public static ParticleSystem ExplosionSmokeParticleSystem
        {
            get { return instance.explosionSmokeParticleSystem; }
        }
    }
}
